package com.example.side_pull_menu.Fragment;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.side_pull_menu.Fragmentkj2;
import com.example.side_pull_menu.Fragmentkj7;
import com.example.side_pull_menu.Fragmentkj7_two;
import com.example.side_pull_menu.R;
import com.google.gson.Gson;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

import static android.support.constraint.Constraints.TAG;

public class Fragment_two extends Fragment {
    private TextView expenditure;
    private TextView time1;
    private TextView week;
    private TextView people;
    private TextView license_plate;
    private TextView echarge;
    private TextView balance;
    private TextView echarge_time;
    private TextView balance_time1;
    private ListView two_lv;
    private double j=0;
    private List<Fragmentkj7_two>fragmentkj2ss=new ArrayList<>();
    private List<Fragmentkj7_two.DataBean>fragmentkj2s=new ArrayList<>();
    private BaseAdapter adapter;
    public Fragment_two() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_two1, container, false);
        initView(view);
        initData(view);
        return view;
    }

    public interface BlogService {
        @GET("getchargelog")
        Call<ResponseBody> getBlog();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private void initView(View view) {
        two_lv=(ListView)view.findViewById(R.id.two_lv);
        expenditure = (TextView) view.findViewById(R.id.expenditure);
        time1 = (TextView) view.findViewById(R.id.time1);
        week = (TextView) view.findViewById(R.id.week);
        people = (TextView) view.findViewById(R.id.people);
        license_plate = (TextView) view.findViewById(R.id.license_plate);
        echarge = (TextView) view.findViewById(R.id.echarge);
        balance = (TextView) view.findViewById(R.id.balance);
        echarge_time = (TextView) view.findViewById(R.id.echarge_time);
        balance_time1 = (TextView) view.findViewById(R.id.balance_time1);
    }
    private void initData(View view){
        Retrofit retrofit=new Retrofit.Builder()
                //设置目标的网络地址
                .baseUrl("http://hh1.me:5001/")
                .build();
        Fragment_two.BlogService service=retrofit.create(Fragment_two.BlogService.class);
        Call<ResponseBody>call=service.getBlog();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Gson gson = new Gson();
                    Fragmentkj7_two carlist = gson.fromJson(response.body().string(), Fragmentkj7_two.class);
                    fragmentkj2ss.add(carlist);
                    for (int i = 0; i <fragmentkj2ss.get(0).getData().size() ; i++) {
                        if (fragmentkj2ss.get(0).getData().get(i).getWho().equals("张三")){
                            j= j+Double.parseDouble(fragmentkj2ss.get(0).getData().get(i).getCharge());
                            fragmentkj2s.add(fragmentkj2ss.get(0).getData().get(i));
                        }
                    }
                   two_lv.setAdapter(new MeAdapter1());
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }
    public static String getWeek(String time){
        String Week="";
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c=Calendar.getInstance();
        try{
            c.setTime(format.parse(time));
        }catch (ParseException e){
            e.printStackTrace();
        }
        int wek=c.get(Calendar.DAY_OF_WEEK);
        if (wek == 1) {
            Week += "星期日";
        }
        if (wek == 2) {
            Week += "星期一";
        }
        if (wek == 3) {
            Week += "星期二";
        }
        if (wek == 4) {
            Week += "星期三";
        }
        if (wek == 5) {
            Week += "星期四";
        }
        if (wek == 6) {
            Week += "星期五";
        }
        if (wek == 7) {
            Week += "星期六";
        }
        Log.i("TAG","123"+Week);
        return Week;
    }
    private class MeAdapter1 extends BaseAdapter{

        @Override
        public int getCount() {
            return fragmentkj2s.size();
        }
        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1=(View)View.inflate(getContext(),R.layout.fragment_two,null);
            TextView time1=view1.findViewById(R.id.time1);
            TextView people=view1.findViewById(R.id.people);
            TextView week=view1.findViewById(R.id.week);
            TextView license_plate=view1.findViewById(R.id.license_plate);
            TextView echarge=view1.findViewById(R.id.echarge);
            TextView balance=view1.findViewById(R.id.balance);
            TextView balance_time1=view1.findViewById(R.id.balance_time1);
            expenditure.setText("总支出:"+j);
            people.setText("充值人:"+fragmentkj2s.get(i).getWho());
            time1.setText(fragmentkj2s.get(i).getTime());
            week.setText(getWeek(fragmentkj2s.get(i).getTime()));
            license_plate.setText("车牌号:"+fragmentkj2s.get(i).getCarno());
            echarge.setText("充值:"+fragmentkj2s.get(i).getCharge());
            balance.setText("余额"+fragmentkj2s.get(i).getAmount());
            balance_time1.setText(fragmentkj2s.get(i).getTime());
            return view1;
        }
    }
}

